package com.example;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.database.StudentMessage;
import com.example.entity.Student;
import com.example.view.Customize;

public class SettingActivity extends AppCompatActivity {
    private Customize customize ;
    private LinearLayout updatePassword ;
    private LinearLayout exitLogin ;
    private Switch switchNotification ;
    private Switch switchAdminMode ;
    private LinearLayout addStudent ;
    private LinearLayout addCourse ;
    private View view_sc ;
    @SuppressLint("MissingInflatedId")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        updatePassword=findViewById(R.id.updatePassword);
        exitLogin=findViewById(R.id.exitLogin);
        switchNotification=findViewById(R.id.switchNotification);
        switchAdminMode=findViewById(R.id.switchAdminMode);
        addStudent=findViewById(R.id.addStudent);
        addCourse=findViewById(R.id.addCourse);
        view_sc=findViewById(R.id.view_sc);
        customize = findViewById(R.id.settingTop);
        view_sc.setVisibility(View.GONE);
        addStudent.setVisibility(View.GONE);
        addCourse.setVisibility(View.GONE);


        customize.setTitleText("设置");
        customize.setLeftBtnIsVisible(true);
        customize.setRightBtnNoVisible(true);
        customize.setLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //获取登录页面传递的登录信息并通过查询id来查询班级
        StudentMessage studentMessage = new StudentMessage(this);

        // Update Password Button Click Event
        updatePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, UpdatePasswordActivity.class);
                startActivity(intent);
            }
        });

// Exit Login Button Click Event
        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutConfirmationDialog();
            }
        });

// Switch for Notification Click Event
        switchNotification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // User turned on notifications, send a notification here
                    Toast.makeText(SettingActivity.this, "通知功能已打开", Toast.LENGTH_SHORT).show();

                    sendNotification();
                } else {
                    // User turned off notifications, handle accordingly
                    Toast.makeText(SettingActivity.this, "通知功能已关闭", Toast.LENGTH_SHORT).show();
                    // No action needed if not sending anything on disabling
                }
            }
        });
        //获取登录页面传递的登录信息
        SharedPreferences sharedPreferences = getSharedPreferences("loginIdAndName" , Context.MODE_PRIVATE);
        String studentId = sharedPreferences.getString("studentId", "");

//        if (course.equals("GL000")) {
        switchAdminMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                boolean isEnrolled = studentMessage.isStudentEnrolled(studentId);
                if (isEnrolled){
                    if (isChecked){
                        // 根据开关状态显示或隐藏addStudent和addCourse
                        addStudent.setVisibility(View.VISIBLE);
                        addCourse.setVisibility(View.VISIBLE);
                        view_sc.setVisibility(View.VISIBLE);
//                    addStudent.setVisibility(isChecked ? View.VISIBLE : View.GONE);
//                    addCourse.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                        Toast.makeText(SettingActivity.this, "管理员模式已开启", Toast.LENGTH_SHORT).show();
                    }else {
                        addStudent.setVisibility(View.INVISIBLE);
                        addCourse.setVisibility(View.INVISIBLE);
                        view_sc.setVisibility(View.INVISIBLE);
                        Toast.makeText(SettingActivity.this, "管理员模式已关闭", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    if (isChecked){
                        switchAdminMode.setChecked(false);
                        Toast.makeText(SettingActivity.this, "无管理员权限", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

        addStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this , AddStudentActivity.class);
                startActivity(intent);
            }
        });
        addCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this , AddCurriculum.class);
                startActivity(intent);
            }
        });
    }

    private void sendNotification() {
    }
    private void showLogoutConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击确定，执行退出登录操作
                        logout();
                    }
                })
                .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                .show();
    }

    private void logout() {
        // 关闭所有Activity并回到登录界面
        // 获取当前Activity的Intent
        Intent loginIntent = new Intent(SettingActivity.this, LoginActivity.class);
        // 确保使用FLAG_ACTIVITY_CLEAR_TOP，这样当新的Intent启动时，如果在任务栈中有该Activity的实例，则不会重新创建，而是重用这个Activity实例
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // 使用finishAffinity来关闭所有栈顶的Activity
        finishAffinity();
        // 使用startActivity启动登录Activity
        startActivity(loginIntent);
        // 如果需要，可以在这里添加一个成功退出登录的Toast提示或其他反馈
    }
}
